<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>总结生命周期</title>
    <!-- 引入 vue -->
    <script type="text/javascript" src="../node_modules/vue/dist/vue.js"></script>
</head>
<body>
    <div id="root">
        <h2 :style="{opacity}">欢迎学习 Vue</h2>
        <button @click="opacity = 1">透明度设置为 1</button>
        <button @click="stop">点我停止变换</button>
    </div>
</body>
<script type="text/javascript">
    new Vue({
        el: '#root',
        data: {
            opacity: 1
        },
        methods: {
            stop() {
                this.$destroy();
            }
        },
        mounted() {
            console.log('mounted', this);
            this.timer = setInterval(() => {
                console.log('setInterval');
                this.opacity -= 0.01;
                if (this.opacity <= 0)
                    this.opacity = 1;
            }, 16);
        },
        beforeDestroy() {
            clearInterval(this.timer);
            console.log('vm即将被杀死');
        }
    })
</script>
</html>